package com.sdnu.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdnu.common.Result.R;
import com.sdnu.service.entity.ucenter.FeedbackRecord;
import com.sdnu.service.entity.ucenter.User;
import com.sdnu.service.entity.ucenter.vo.FeedBackRecordListVo;
import com.sdnu.service.service.FeedbackRecordService;
import com.sdnu.service.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhao YunLai
 * @since 2021-12-05
 */
@RestController
@RequestMapping("/ucenter/feedback_record")
@CrossOrigin
public class FeedbackRecordController {

    @Autowired
    private FeedbackRecordService feedbackRecordService;
    @Autowired
    private UserService userService;


    @ApiOperation("分页查询")
    @GetMapping("pageSelect/{current}/{limit}")
    public R pageSelect(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable int current,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable int limit
    ){
        Page<FeedbackRecord> page = new Page<>(current, limit);
        QueryWrapper<FeedbackRecord> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");

        List<FeedBackRecordListVo> finalList = feedbackRecordService.pageSelect(page, wrapper);

        //经过处理，原始数据封装在page中
        //total是所有的记录数，不是records中的记录数
        long total = page.getTotal();

        return R.ok().data("total",total).data("items",finalList);
    }

    @ApiOperation("得到所有反馈")
    @GetMapping("getAll")
    public R getAll(){
        List<FeedBackRecordListVo> list = feedbackRecordService.getAll();
        return R.ok().data("items",list);
    }

    @PostMapping("add")
    public R addRecord(@RequestBody FeedbackRecord feedbackRecord){
        feedbackRecordService.save(feedbackRecord);
        String userId = feedbackRecord.getUserId();
        User byId = userService.getById(userId);
        //日志
        System.out.println(new Date()+":"+byId.getName()+"-->提交了反馈：{类型："+feedbackRecord.getType()+",content:"+feedbackRecord.getContent()+"}");
        return R.ok();
    }

    @PutMapping("update")
    public R updateRecord(@RequestBody FeedbackRecord feedbackRecord){
        boolean b = feedbackRecordService.updateById(feedbackRecord);
        return b?R.ok():R.error();
    }

    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = feedbackRecordService.removeById(id);
        return b?R.ok():R.error();
    }

    @GetMapping("get/{id}")
    public R getRecord(@PathVariable String id){
        FeedbackRecord item = feedbackRecordService.getById(id);
        return R.ok().data("item",item);
    }

}

